﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Billing.DataObjects;

namespace Billing.Win
{
    public partial class ClientInfoForm : Billing.WindowsBase.FormBase
    {
        public int ClientID
        {
            get
            {
                int clientID = 0;
                int.TryParse(this.valID.Text, out clientID);
                return clientID;
            }

            set
            {
                this.valID.Text = value.ToString();
            }
        }
       
        public ClientInfoForm()
        {
            InitializeComponent();
        }

        BillingDataContext mBillingDataContext;
        public BillingDataContext BillingDataContext
        {
            get
            {
                if (mBillingDataContext == null)
                    mBillingDataContext = new BillingDataContext();
                return mBillingDataContext;
            }
            set { mBillingDataContext = value; }
        }
        private void CleareFields()
        {
            this.valID.Text = string.Empty;
            this.valName.Text = string.Empty;
            this.valIdentificationNumber.Text = string.Empty;
            this.valPhoneNumber.Text = string.Empty;
        }

        public void Search()
        {
            OnSearch();
        }

        private int SelectedClientID;

        private void btRetry_Click(object sender, EventArgs e)
        {
            this.CleareFields();
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Enter)
            {
                OnSearch();
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void OnSearch()
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                this.bsClientInfo.Clear();
                this.bsBalances.Clear();
                this.grpClentInfo.Enabled = false;


                this.bsSearchDetail.DataSource = this.BillingDataContext.ClientSearchDetailSelect(
                                                                                            ClientID,
                                                                                            this.valIdentificationNumber.Text,
                                                                                            this.valName.Text,
                                                                                            this.valPhoneNumber.Text);
                if (bsSearchDetail.Count == 1)
                {
                    if (this.valPhoneNumber.Text.Trim() != string.Empty)
                        this.valPhoneNumber.Items.Insert(0, this.valPhoneNumber.Text, this.valPhoneNumber.Text);

                    OnSearchDetail();
                    this.grSearchDetail.Expanded = false;
                }
                else if (bsSearchDetail.Count > 0)
                {
                    if (this.valPhoneNumber.Text.Trim() != string.Empty)
                        this.valPhoneNumber.Items.Insert(0, this.valPhoneNumber.Text, this.valPhoneNumber.Text);
                    this.grdSearchDetail.Rows[0].Selected = true;
                    this.grdSearchDetail.Rows[0].Activate();
                    this.grSearchDetail.Expanded = true;
                }
                this.Cursor = Cursors.Default;
            }
            catch
            {
                this.Cursor = Cursors.Default;
            }

        }
        private void OnSearchDetail()
        {
            if (bsSearchDetail.Count > 0)
            {
                SelectedClientID = ((ClientSearchDetailSelectResult)this.bsSearchDetail.Current).ID;
                this.bsClientInfo.DataSource = this.BillingDataContext.ClientInfoSelect(SelectedClientID);
                this.bsBalances.DataSource = this.BillingDataContext.BalancesSelect(SelectedClientID);
                this.grpClentInfo.Enabled = true;

            }
        }

        private void btSearch_Click(object sender, EventArgs e)
        {
            this.OnSearch();
        }

        private void btnPhones_Click(object sender, EventArgs e)
        {
            DoShowReport("/BillingReports/SubClientPhoneNumbers", SelectedClientID);
        }

        private void btnInvoices_Click(object sender, EventArgs e)
        {
            DoShowReport("/BillingReports/SubInvoices", SelectedClientID);
        }

        private void DoShowReport(string reportName, int clientID)
        {
            try
            {
                Billing.Reports.ReportForm df = new Billing.Reports.ReportForm(reportName);
                df.StartPosition = FormStartPosition.CenterScreen;
                df.Text = string.Format("{0} - {1}", clientID, reportName);
                df.PrepareForSelectDialogStyle();
                df.AddParameter("ClientID", clientID.ToString(), false);
                df.LoadReport();
                df.Show();
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error");
            }
        }
        private void DoShowStatementReport(string reportName, int clientID)
        {
            try
            {
                Billing.Reports.ReportForm df = new Billing.Reports.ReportForm("/BillingReports/PaymentAnalyses/StatementSelectByPeriod");
                df.StartPosition = FormStartPosition.CenterScreen;
                df.Text = string.Format("{0} - {1}", clientID, reportName);
                df.PrepareForSelectDialogStyle();
                df.AddParameter("ClientID", clientID.ToString(), false);
                df.LoadReport();
                df.Show();
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error");
            }
        }

        private void grdSearchDetail_DoubleClickRow(object sender, Infragistics.Win.UltraWinGrid.DoubleClickRowEventArgs e)
        {
            this.OnSearchDetail();
        }

        private void ClientInfoForm_Load(object sender, EventArgs e)
        {

        }

        private void valName_Enter(object sender, EventArgs e)
        {
            Billing.KeyboardSwitcher.SwitchKeyboardLayoutToKA(true);
        }

        private void btnPayments_Click(object sender, EventArgs e)
        {
            DoShowReport("/BillingReports/PaymentAnalyses/PaymentsSelectByPeriod", SelectedClientID);
        }

        private void btnStatment_Click(object sender, EventArgs e)
        {
            ClientStatementsForm frm = new ClientStatementsForm();
            frm.ClientID = this.SelectedClientID;
            frm.ShowDialog();
        }

        private void ultraGrid1_ClickCellButton(object sender, Infragistics.Win.UltraWinGrid.CellEventArgs e)
        {

            ClientStatementsForm frm = new ClientStatementsForm();
            frm.ClientID = this.SelectedClientID;
            frm.DateTo = (DateTime)e.Cell.Row.Cells["BalanceDate"].Value;
            frm.DateFrom = this.BillingDataContext.GetLastPaymentDate(this.SelectedClientID).Value;
            if (frm.DateFrom > frm.DateTo)
                frm.DateFrom = frm.DateTo;
            frm.ShowDialog();
        }

        private void btnCharge_Click(object sender, EventArgs e)
        {
            ClientChargesForm frm = new ClientChargesForm();
            frm.ClientID = this.SelectedClientID;
            frm.ShowDialog();
        }

        private void btnChargeDetail_Click(object sender, EventArgs e)
        {
            DoShowReport("/BillingReports/ClientAccountsCharges", SelectedClientID);
        }

        private void btnClientTurn_Click(object sender, EventArgs e)
        {
            DoShowReport("/BillingReports/ClientTurn", SelectedClientID);
        }
    }
}
